package com.tiantianfresh.admin.controller;

import com.tiantianfresh.admin.service.AllocationService;
import com.tiantianfresh.model.pojo.CommodityAllocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/allocation")
public class AllocationController {
    @Autowired
    private AllocationService allocationService;

    //查询全部的Allocation
    @RequestMapping("/all")
    public String all(Model model){
        List<CommodityAllocation> list=allocationService.selectAll();
        model.addAttribute("list",list);
        return "all";
    }
    //跳转到添加书籍页面
    @RequestMapping("/toadd")
    public String add(){
        return "add";
    }
    //添加书籍
    @RequestMapping("/add")
    public String add(CommodityAllocation allocation){
        allocationService.addAllocation(allocation);
        return "redirect:/allocation/all";
    }
    //跳转到修改书籍页面
    @RequestMapping("/toupdate/{id}")
    public String update(@PathVariable int id,Model model){
        CommodityAllocation allocation=allocationService.queryAllocationById(id);
        model.addAttribute("Allocation",allocation);
        return "update";
    }
    //保存修改信息
    @RequestMapping("/update")
    public String update(CommodityAllocation allocation){
       allocationService.updateAllocation(allocation);
        return "redirect:/allocation/all";
    }
    //删除书籍
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable int id){
        allocationService.deleteAllocationById(id);
        return "redirect:/allocation/all";
    }
}
